package Model;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

/*
 * Purpose model ball
 * Has direction position and color and size
 */
/**
 *
 * @author Pro
 */
public class Ball {

    private static Random random = new Random();
    private int size, defaultSize;
    private boolean defaultDirectX;
    private boolean defaultDirectY;
    private Color color;
    private int x, y;
    private int xOff, yOff;

    public Ball() {
        defaultSize = 15;
        int cValue = random.nextInt(0xffffff);
        int sValue = defaultSize + random.nextInt(10);
        defaultDirectX = random.nextBoolean();
        defaultDirectY = random.nextBoolean();

        color = new Color(cValue);
        size = sValue;
        xOff = 1 + random.nextInt(5);

        if (defaultDirectX) {
            xOff *= -1;
        }

        yOff = 1 + random.nextInt(5);
        
        if (defaultDirectY) {
            yOff *= -1;
        }

    }

    public Ball(int size, Color color) {
        this.size = size;
        this.color = color;
    }

    public void place(int x, int y) {

        this.x = x;
        this.y = y;

    }

    public void draw(Graphics g) {

        g.setColor(color);
        g.fillOval(x, y, size, size);

    }

    public void bounce(int width, int height) {

        int change;

        if ((y < 0 && yOff < 0)) {

      //      System.out.println("Berfore  y:" + y);


            change = random.nextInt(5);

            while (change == 0) {
                change = random.nextInt(5);

            }

            yOff = change; // bounce

        }

        if ((y > (height - size) && yOff > 0)) {
 //           System.out.println("Berfore  y:" + y);

            change = random.nextInt(5);

            while (change == 0) {

                change = random.nextInt(5);

            }

            yOff = change * -1; // bounce

        }

//        System.out.println("After  y:" + y + "  yOff" + yOff);

        if (x < (width/2-30) && xOff < 0) {

    //        System.out.println("Berfore x:" + x);

            change = random.nextInt(3);

            while (change == 0) {
                change = random.nextInt(3);

            }

            xOff = change; // bounce

        }

        if ((x > ((width -width/2)+30) && xOff > 0)) {
  //          System.out.println("Berfore  x:" + x);


            change = random.nextInt(5);

            while (change == 0) {

                change = random.nextInt(5);

            }

            xOff = change * -1; // bounce

        }

  //      System.out.println("After x:" + x);

    }

    public void move() {

        this.x += xOff;
        this.y += yOff;

//        System.out.println("MOVE x:" + x + "  y:" + y + "  yOff" + yOff);

    }
}
